from django.db import models
from utils import _to_chinese4

'''
# 表: 书表, 文章表, 章节表, 作者表
# 书表: 
    书名, 书封面, 作者, 总字数(优化), 总点击数(优化)
# 书分类:
    书名, 书封面, 书状态, 总字数(优化), 总点击数(优化), 作者(外键), 分类(外键)
# 文章表: 
    标题, 详情, 内容, 字数, 点击数, 当前章节, 书(外键)
# 作者表
    作者姓名
'''


class Category(models.Model):
    """书分类名"""
    name = models.CharField(max_length=64, verbose_name='书分类名')

    is_delete = models.BooleanField(default=False, verbose_name='是否删除')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='最后更新时间')

    class Meta:
        verbose_name_plural = '书分类表'

    def __str__(self):
        return self.name

    @property
    def book_list(self):
        li = []
        for book in self.book_set.all():
            li.append({
                'id': book.id,
                'name': book.name,
                'book_img': book.book_img,
                'author_name': book.author_name,
            })
        return li


class Book(models.Model):
    """书名, 书封面, 书状态, 总字数(优化), 总点击数(优化), 作者(外键), 分类(外键)"""
    name = models.CharField(max_length=64, verbose_name='书名')
    book_img = models.CharField(max_length=255, verbose_name='书封面')

    status_choices = [(0, '连载'), (1, '完结')]
    status = models.IntegerField(choices=status_choices, default=0, verbose_name='书的状态')

    # 详情因该放在书里面
    desc = models.CharField(max_length=255, null=True, verbose_name='文章详情')

    # 数据库优化字段
    total_word_number = models.IntegerField(verbose_name='总字数', default=0)
    total_up_num = models.IntegerField(verbose_name='总点击数', default=0)
    total_section = models.IntegerField(default=0, verbose_name='总章节数')

    # 外键: 书没了分类还要在, 作者没了书还要在
    category = models.ForeignKey(to='Category', on_delete=models.DO_NOTHING, db_constraint=False, verbose_name='书所属分类')
    author = models.ForeignKey(to='Author', on_delete=models.DO_NOTHING, db_constraint=False, verbose_name='书的作者')

    is_free = models.BooleanField(default=True, verbose_name='当前书籍是否免费')

    is_delete = models.BooleanField(default=False, verbose_name='是否删除')
    is_show = models.BooleanField(default=True, verbose_name='是否展示')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='最后更新时间')

    class Meta:
        verbose_name_plural = '书表'

    def __str__(self):
        return self.name

    @property
    def category_name(self):
        return self.category.name

    @property
    def author_name(self):
        return self.author.name

    @property
    def current_status(self):
        return self.get_status_display()

    @property
    def article_list(self):
        article_list = self.article_set.all()
        li = []
        for article in article_list:
            li.append({
                'id': article.id,
                'title': article.title,
                'content': article.content,
                'word_number': article.word_number,
                'up_num': article.up_num,
                'current_section': article.current_section,
                'created_time': article.current_section,
                'updated_time': article.current_section,
                'current_section_name': article.current_section_name,
                'is_free': article.is_free,
            })
        return li


class Article(models.Model):
    """标题, 详情, 内容, 字数, 点击数, 当前章节, 书(外键)"""
    title = models.CharField(max_length=32, verbose_name='文章标题')
    content = models.TextField(verbose_name='文章内容')
    word_number = models.IntegerField(verbose_name='文章字数')
    up_num = models.IntegerField(default=0, verbose_name='文章点击数')
    current_section = models.IntegerField(null=True, verbose_name='当前章节')
    is_free = models.BooleanField(default=True, verbose_name='当前文章是否免费')

    is_delete = models.BooleanField(default=False, verbose_name='是否删除')
    is_show = models.BooleanField(default=True, verbose_name='是否展示')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='最后更新时间')

    # 外键: 书没了章节也没有存在的必要了
    book = models.ForeignKey(to='Book', on_delete=models.CASCADE, db_constraint=False, verbose_name='文章所属的书')

    class Meta:
        verbose_name_plural = '文章表'

    def __str__(self):
        return self.title

    @property
    def current_section_name(self):
        return f'第{_to_chinese4(self.current_section)}章'


class Author(models.Model):
    """作者姓名"""
    name = models.CharField(max_length=64, verbose_name='作者姓名')

    is_delete = models.BooleanField(default=False, verbose_name='是否删除')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name_plural = '作者表'

    def __str__(self):
        return self.name